package com.xinke.edu.mypetadoption.fragment3;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.xinke.edu.mypetadoption.R;
import com.xinke.edu.mypetadoption.databinding.FragmentSz2Binding;
import com.xinke.edu.mypetadoption.databinding.FragmentSzBinding;
import com.xinke.edu.mypetadoption.entity.MerchantInfoDTO;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


public class SgFragment extends Fragment {

    //管理员个人界面

    FragmentSz2Binding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentSz2Binding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loaddata();

    }



    private void loaddata(){

        //获取用户ip
        int key = 1;

        RetrofitUtil.getApi().getMerchantInfo(key)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<MerchantInfoDTO>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {}

                    @Override
                    public void onNext(@NonNull ResultBody<MerchantInfoDTO> result) {

                        MerchantInfoDTO info = result.getData();
                        if (info != null) {

                            ImageView img = getView().findViewById(R.id.dptx);
                            Glide.with(getContext()).load(info.getAvatar()).into(img);

                            binding.dpmc.setText(info.getContactPerson());
                            binding.dpdh.setText(info.getContactPhone());


                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(requireContext(), "加载失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {}
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }



}